package com.c.ctgproject.ui.production.material

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.mode.MyConstants
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.utils.handlerNull
import com.c.ctgproject.utils.initData
import com.c.ctgproject.utils.showToast
import kotlinx.android.synthetic.main.activity_material_input_weight.*

/**
 *作者:LiBW
 *创建日期: 2020/10/13
 *描述: 物料 录入重量
 */
class MaterialInputWeightActivity : BaseActivity() {
    private val materialName: String by lazy { intent.getStringExtra(MyConstants.MATERIAL_NAME) }


    private val mViewModel: MaterialInputWeightViewModel by lazy {
        ViewModelProvider(this)[MaterialInputWeightViewModel::class.java]
    }

    override fun event() {
        changeTransparentStatusBar()
        mMaterialNameTV.text = materialName.handlerNull()
        mViewModel.error.observe(this, Observer {
            loading.dismiss()
            MyLog.d("错误:$it")
            showToast(it)
        })

        mViewModel.result.observe(this, Observer {
            loading.dismiss()
            MyLog.d("提交重量:$it")
            val data = initData(it)
            if (data.status == 0) {
                showToast("操作成功")
                finish()
            } else {
                showToast(data.msg)
            }
        })
    }

    override fun click() {
        //完成
        mConfirmBTN.setOnClickListener {
            val weight = mMaterialWeightET.text.toString()
            if (weight.isEmpty()) {
                showToast("请输入物料重量")
            } else {
                mViewModel.inputMaterialWeight(materialName, weight)
            }
        }
    }

    override fun getLayout() = R.layout.activity_material_input_weight

}